//
//  XiaoximenVotePartyInfoViewController.m
//  Xiaoximen_iOS
//
//  Created by Wang Zhongyu on 12-7-19.
//  Copyright (c) 2012年 Peking University. All rights reserved.
//

#import "XiaoximenVotePartyInfoViewController.h"
#import "XiaoximenTimePlaceVoteViewController.h"
#import "XiaoximenAppDelegate.h"

@interface XiaoximenVotePartyInfoViewController () {
	int detailPersonID;
}

@end

@implementation XiaoximenVotePartyInfoViewController
@synthesize party;
@synthesize images;
@synthesize titleLabel;
@synthesize hostLabel;

- (void)addAllPersonsInfo
{
	if ([[NSUserDefaults standardUserDefaults] objectForKey:@"user_list"] == nil) return ;
	NSArray *allPersons = [[NSUserDefaults standardUserDefaults] objectForKey:@"user_list"];
	for (NSDictionary *personInfo in allPersons) {
		NSString *user_name = [personInfo objectForKey:@"user_name"];
		NSString *user_cellphone = [personInfo objectForKey:@"user_cellphone"];
		int user_id = [[personInfo objectForKey:@"user_id"] intValue];
		[party addPersonWithPhoneString:user_cellphone userName:user_name personID:user_id];
	}
}

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view.
	self.titleLabel.text = party.title;
	self.hostLabel.text = [NSString stringWithFormat:@"发起人: %@", party.host];
	
	[self addAllPersonsInfo];
	
	images = [[NSMutableArray alloc] init];
	for (int i = 0; i < party.persons.count; i++) {
		UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 200, 43)];
		imageView.image = [UIImage imageNamed:@"textfield"];
		[images addObject:imageView];
	}
}

- (void)viewDidUnload
{
	[self setTitleLabel:nil];
	[self setHostLabel:nil];
    [super viewDidUnload];
    // Release any retained subviews of the main view.
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
	return party.persons.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"personIdentifier";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    
	if ([[party.persons objectAtIndex:indexPath.row] isVoted]) {
		UIImageView * imageView = [images objectAtIndex:indexPath.row];
		imageView.image = [UIImage imageNamed:@"text"];
	}
	[cell addSubview:[images objectAtIndex:indexPath.row]];
	
	UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(20, 11, 180, 21)];
	label.text = [[party.persons objectAtIndex:indexPath.row] name];
	if ([[party.persons objectAtIndex:indexPath.row] isVoted]) {
		if ([[party.persons objectAtIndex:indexPath.row] isComing]) {
			label.text = [label.text stringByAppendingString:@" (参与)"];
		} else {
			label.text = [label.text stringByAppendingString:@" (不参与)"];
		}
	} else 
		label.text = [label.text stringByAppendingString:@" (未投票)"];
	label.font = [UIFont boldSystemFontOfSize:17];
	label.backgroundColor = [UIColor colorWithWhite:1.0 alpha:0.0];
	[cell addSubview:label];
	
    return cell;
}

/*
 // Override to support conditional editing of the table view.
 - (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
 {
 // Return NO if you do not want the specified item to be editable.
 return YES;
 }
 */

/*
 // Override to support editing the table view.
 - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
 {
 if (editingStyle == UITableViewCellEditingStyleDelete) {
 // Delete the row from the data source
 [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
 }   
 else if (editingStyle == UITableViewCellEditingStyleInsert) {
 // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
 }   
 }
 */

/*
 // Override to support rearranging the table view.
 - (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath
 {
 }
 */

/*
 // Override to support conditional rearranging of the table view.
 - (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
 {
 // Return NO if you do not want the item to be re-orderable.
 return YES;
 }
 */

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
	XiaoximenTimePlaceVoteViewController *tpvvc  = segue.destinationViewController;
	tpvvc.party = party;
	if (detailPersonID == -1) {
		tpvvc.canVote = YES;
		
		NSString *myName = [[NSUserDefaults standardUserDefaults] valueForKey:kNAME];
		int myID = [[[NSUserDefaults standardUserDefaults] valueForKey:kID] intValue];
		for (XiaoximenPerson *person in party.persons) {
			if ([person.name isEqualToString:myName] && person.ID == myID) {
				tpvvc.person = person;
			}
		}
	} else {
		tpvvc.canVote = NO;
		tpvvc.person = [party.persons objectAtIndex:detailPersonID];
	}
}

#pragma mark - Table view delegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
	if ([[party.persons objectAtIndex:indexPath.row] isVoted]) {
		detailPersonID = indexPath.row;
		[self performSegueWithIdentifier:@"VoteSegue" sender:self];
	}
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}

- (IBAction)onBack:(id)sender {
	[self.navigationController popViewControllerAnimated:YES];
}

- (IBAction)onVote:(id)sender {
	detailPersonID = -1;
	[self performSegueWithIdentifier:@"VoteSegue" sender:self];
}
@end
